﻿
CREATE PROCEDURE [dbo].[GoodsVsContragentsUpdate]
(
	@GoodsId int,
	@ContragentId int,
	@GoodsCode int,
	@GoodsName varchar(150),
	@CountryName varchar(20),
	@ManufacturerName varchar(70),
	@Note varchar(250),
	@MD datetime,
	@MW varchar(50),
	@ToDelete bit,
	@Original_Id int,
	@Original_MD datetime
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @orgContragentId int, @orgGoodsCode int, @orgToDelete bit, @orgParentToDelete bit
DECLARE @orgGoodsName varchar(150), @orgCountryName varchar(20), @orgManufacturerName varchar(70)

SET NOCOUNT ON

IF (@GoodsId Is Null) OR (@ContragentId Is Null) OR (@GoodsCode Is Null) OR (@GoodsName Is Null)
OR (@ToDelete Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF (ltrim(@GoodsName) = '')
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Незаполнено наименование товара у контрагента!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

SELECT @orgContragentId = ContragentId, @orgGoodsCode = GoodsCode,
@orgGoodsName = GoodsName, @orgCountryName = CountryName, @orgManufacturerName = ManufacturerName,
@orgToDelete = ToDelete
FROM [CA].[GoodsVsContragents] WHERE Id = @Original_Id AND MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 3, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @orgParentToDelete = ToDelete
FROM CA.Goods WHERE Id = @GoodsId
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 4, 
	@strMsg = 'Отсутствует родительская запись с кодом ' + cast(@GoodsId as varchar(20)) + ' в справочнике товаров!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @orgParentToDelete = 1 
BEGIN
	SELECT @intErrCode = 5, 
	@strMsg = 'Родительская запись с кодом ' + cast(@GoodsId as varchar(20)) + ' в справочнике товаров отмечена для удаления!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @orgToDelete = 1 --Запись была отмечена для удаления
BEGIN
	IF @ToDelete = 1 
	BEGIN
		SELECT @intErrCode = 6, 
		@strMsg = 'Нельзя редактировать запись, отмеченную для удаления!'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END ELSE BEGIN
	IF @ToDelete = 1 
	BEGIN
		SELECT @intErrCode = 7, 
		@strMsg = 'Для удаления данных должна использоваться другая процедура!'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END

IF @orgContragentId <> @orgContragentId 
BEGIN
	SELECT @intErrCode = 8, 
	@strMsg = 'Нельзя менять контрагента!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF (@orgContragentId <> @ContragentId) OR (@orgGoodsCode <> @GoodsCode) OR (@orgGoodsName <> @GoodsName)
OR (dbo.VariantDiff(@orgCountryName, @CountryName) = 1) OR (dbo.VariantDiff(@orgManufacturerName, @ManufacturerName) = 1)
BEGIN
	IF EXISTS (SELECT TOP 1 ID FROM [CA].[GoodsVsContragents] 
	WHERE ContragentId = @ContragentId AND GoodsCode = @GoodsCode 
	AND GoodsName = @GoodsName 
	AND ( (CountryName = @CountryName) OR (@CountryName Is Null AND CountryName Is Null) )
	AND ( (@ManufacturerName = ManufacturerName) OR (@ManufacturerName Is Null AND ManufacturerName Is Null) )
	AND ToDelete = 0 )
	BEGIN
		SELECT @intErrCode = 9, @strMsg = 'Дублируется товар у контрагента!'
		RAISERROR (@strMsg, 11, @intErrCode)
		RETURN @intErrCode
	END
END

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
	SET NOCOUNT OFF;
UPDATE [CA].[GoodsVsContragents] SET [GoodsId] = @GoodsId, [ContragentId] = @ContragentId, [GoodsCode] = @GoodsCode, [GoodsName] = @GoodsName, [CountryName] = @CountryName, [ManufacturerName] = @ManufacturerName, 
[Note] = @Note, [MD] = @MD, [MW] = @MW, ToDelete = @ToDelete 
WHERE Id = @Original_Id;
	
SELECT Id,  GoodsId, ContragentId, GoodsCode, GoodsName, CountryName, ManufacturerName, Note, MD, MW, ToDelete 
FROM CA.GoodsVsContragents WHERE (Id = @Original_Id)

RETURN 0
